
/** 将对象拼接成urlSearchParams字符串 */
export function objectToStr(obj: Record<string, any>): string {
    let str = "";
    if (!Object.keys(obj).length) return str;
    str += "?";
    Object.keys(obj).forEach(key => {
        str += `${key}=${obj[key]}&`
    })
    str = str.slice(0, str.length - 1);
    return str;
}

/** 将urlSearchParams字符拼接成串对象 */
export function searchURLToObject<T>(searchURL?: string): T {
    let url = searchURL || location.hash;
    url = url.split('?')[1];
    if (url) url = `?${url}`
    const urlSearch = new URLSearchParams(url);
    const result = {};
    for (const [key, value] of urlSearch.entries()) {
        Reflect.set(result, key, value);
    }
    return result as T;
}

/** 将urlSearchParams字符拼接成串对象 */
export const getUrlPararms = searchURLToObject;